Page({
  data: {
    banners: [],
    categories: [],
    recommendedTechnicians: [],
    hotServices: [],
    loading: true  // 添加加载状态
  },

  onLoad: function() {
    // 页面加载时可以请求真实数据
    this.loadIndexData();
  },

  loadIndexData: function() {
    const api = require('../../utils/api.js');
    
    console.log('[首页] 开始加载数据...');
    
    // 使用统一的API请求方法
    api.request({
      url: '/services/app-images/',
      method: 'GET'
    }).then(res => {
      console.log('[首页] API返回数据:', res);
      if (res.statusCode === 200) {
        const data = res.data;
        console.log('[首页] 设置数据:', data);
        this.setData({
          categories: data.categories || [],
          banners: data.banners || [],
          recommendedTechnicians: data.technicians || [],
          hotServices: data.services || [],
          loading: false
        });
        console.log('[首页] 数据设置完成');
      } else {
        console.error('[首页] API返回错误状态:', res.statusCode);
        this.setData({ loading: false });
      }
    }).catch(err => {
      console.error('[首页] API请求失败:', err);
      wx.showToast({
        title: '加载数据失败',
        icon: 'none'
      });
      this.setData({ loading: false });
    });
  },

  goToServiceList: function(e) {
    const category = e.currentTarget.dataset.category;
    wx.navigateTo({
      url: `/pages/customer/serviceList?category=${category}`
    });
  },

  goToTechnicianDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/customer/technicianDetail?id=${id}`
    });
  },

  goToServiceDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/customer/serviceDetail?id=${id}`
    });
  },

  goToTechnicianList: function() {
    wx.navigateTo({
      url: '/pages/customer/technicianList'
    });
  },

  goToNearbyTechnicians: function() {
    wx.navigateTo({
      url: '/pages/customer/nearbyTechnicians'
    });
  },

  testNearbyTechnicians: function() {
    // 测试附近技师功能
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        const app = getApp();
        app.request({
          url: '/accounts/location/nearby/',
          method: 'GET',
          data: {
            latitude: res.latitude,
            longitude: res.longitude,
            page: 1,
            page_size: 10
          },
          success: (result) => {
            if (result.statusCode === 200) {
              wx.showModal({
                title: '测试成功',
                content: `找到 ${result.data.count} 位附近技师`,
                showCancel: false
              });
            } else {
              wx.showModal({
                title: '测试失败',
                content: '获取附近技师失败',
                showCancel: false
              });
            }
          },
          fail: () => {
            wx.showModal({
              title: '网络错误',
              content: '请求失败，请检查网络连接',
              showCancel: false
            });
          }
        });
      },
      fail: function() {
        wx.showModal({
          title: '定位失败',
          content: '请检查定位权限设置',
          showCancel: false
        });
      }
    });
  }
})